#include "Analysis.h"


/* void rmdirDispose(char * str,int sockfd){ */
/*     char command[] = "rmdir"; */

/*     Train train; */
/*     train.length = strlen(command);            //向后台发送puts信号 */
/*     memcpy(train.data,command,train.length); */
/*     send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL); */
/*     printf("rmdirsend:%s\n",str); */
/*     train.length = strlen(str);         //发送服务器该传输的位置(父文件路径) */
/*     memcpy(train.data,str,train.length); */
/*     send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL); */


/* } */


/* void rmfileDispose(char * str,int sockfd){ */
/*     char command[] = "remove"; */

/*     Train train; */
/*     train.length = strlen(command);            //向后台发送puts信号 */
/*     memcpy(train.data,command,train.length); */
/*     send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL); */
/*     printf("removesend:%s\n",str); */
/*     train.length = strlen(str);         //发送服务器该传输的位置(父文件路径) */
/*     memcpy(train.data,str,train.length); */
/*     send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL); */

/* } */




char * checkPath(char * str,Stack * s,int flag){   // flag == 1 就是puts gets        
   if(str[strlen(str)-1]=='\n'){
        str[strlen(str)-1] = '\0';
    }

    Stack ass_s;
    stackInit(&ass_s);
     for(int i = 0  ; i <  s->size ; ++i){    //深拷贝
        char * ass_str = (char *)calloc(strlen(s->str[i])+1,sizeof(char));
        strncpy(ass_str,s->str[i],strlen(s->str[i]));
        push(&ass_s,ass_str);
    }

    int nn = 0;
     if(flag == 0){
         while(nn < strlen(str) && str[nn]!=' '){
             ++nn;
         }
         if(nn >= strlen(str)){   //说明指令后没输参数;
             printf("请输入参数\n");
             return NULL;
         }
         while(nn < strlen(str) && str[nn]==' '){
             ++nn;
         }
         if(nn >= strlen(str)){   //说明输入指令后只输了几个空格
             printf("请输入参数\n");
             return NULL;
         }
     }
     if(str[nn] == '/'){                    
         destroyStack(&ass_s);
         char * aaaa = (char *)calloc(2,sizeof(char));
         aaaa[0] = '/';
         push(&ass_s,aaaa);
         if(nn+1==strlen(str)){
             printf("111\n");
             printf("客户端产生服务器要 gets 或 puts 的path:%s\n",aaaa);   
             return aaaa;
         }
         for(int i = nn ; i < strlen(str) ; ++i){
            str[i] = str[i+1];
        }
    }

    if(flag == 0){
        strtok(str," ");    //剔除puts
    }else{
        char * p = strtok(str,"/");   //p 绝对不为空
        if(strcmp(p,"~")==0){
            destroyStack(&ass_s);
            char * bbbb = (char *)calloc(5,sizeof(char));
            bbbb[0] = '/';
            push(&ass_s,bbbb);
        }
        else if(strcmp(p,".")==0){
        }
        else if(strcmp(p,"..")== 0){
            free(top(&ass_s));
            pop(&ass_s);
        }else{
        char * ass_str = (char*)calloc(strlen(p)+1,sizeof(char));
        strncpy(ass_str,p,strlen(p));
        push(&ass_s,ass_str);

        }   
    }
    
    while(!isEmpty(&ass_s)){                   //检查路径是否合理，并得出屏幕应该输出的合理路径。
        char * p = strtok(NULL,"/");
        if(p == NULL){
            break;
        }
        if(strcmp(p,"~")==0){
            destroyStack(&ass_s);
            char * bbbb = (char *)calloc(5,sizeof(char));
            bbbb[0] = '/';
            push(&ass_s,bbbb);
            continue;
        }
        if(strcmp(p,".")==0){
            continue;
        }
        if(strcmp(p,"..")== 0){
            free(top(&ass_s));
            pop(&ass_s);
            continue;
        }
        char * ass_str = (char*)calloc(strlen(p)+1,sizeof(char));
        strncpy(ass_str,p,strlen(p));
        push(&ass_s,ass_str);
    }

    if(isEmpty(&ass_s)){        
        printf("越界错误！\n");
        return NULL;
    }

    if(ass_s.size == 1){
        char * cccc = (char *)calloc(2,sizeof(char));
        cccc[0] = '/';
        printf("222\n");
        printf("客户端产生服务器要 gets 或 puts 的path:%s\n",cccc);   
        destroyStack(&ass_s);
        return cccc;
    }
    //栈不为空，说明路径合理。
    char * path = (char *)calloc(1024,sizeof(char));      //遍历s，得出当前服务器路径(一部分)  /dsf/dsf/fsd
    for(int i = 1 ; i < ass_s.size ; ++i){
        char xiegang[] = "/";
        char sstr[128]={0};
        memcpy(sstr,ass_s.str[i],strlen(ass_s.str[i]));
        strcat(path,xiegang);
        strcat(path,sstr);
    } 
    printf("3333\n");
    printf("客户端产生服务器要 gets 或 puts 的path:%s\n",path);   
    
    return path;
    
}






int analysis(char * str,int sockfd){
    str[strlen(str)-1] = '\0';
    char ass_str[20] = {0};                     //拆出指令
    strncpy(ass_str,str,sizeof(ass_str)-1);
    ass_str[sizeof(ass_str)-1] = '\0';
    char *p = strtok(ass_str," ");
    if(strcmp(p,"cd")==0){                      //解析指令
        return 0;
    }
    if(strcmp(p,"ls")==0){
        return 1;
    }
    if(strcmp(p,"pwd")==0){
        return 2;
    }
    if(strcmp(p,"mkdir")==0){
        return 3;
    }
    if(strcmp(p,"rmdir")==0){
        return 4;
    }
    if(strcmp(p,"remove") == 0){
        return 5;
    }
    if(strcmp(p,"puts")==0){
        return 6;
    }
    if(strcmp(p,"gets")==0){
        return 7;
    }
    return 8;

}
